/*

*/

client
	verb
		try(tier as num)
			set hidden = 1

			var/string
			var/player/p = mob
			for(var/list/l in p.buttons)
				string += l["text"]

			var/book/b = mob.BOOK

			for(var/spell/s in b.spells)
				if(s.name == string)
					spell_error("You've already written that spell in this book!")
					return

			if(!b.check_spells())
				spell_error("Your book is overflowing with magic!")
				return

			var/list/l = generate_spell(lowertext(string), tier ? tier : 1, mob)

			if(l == null || l.len == 0)
				spell_error("The spell was unable to be created.")
				return

			for(var/component/c in l)
				if(!(c.name in p.verified_names))
					p.verified_names += c.name

			var/spell/s = new/spell()
			s.initialize(l, mob)

			if(!s) return

			b.spells[b.check_spells()] = s

			p.update_hotkeys()

			winshow(src, "spell_create", 0)

		remove_spell_button(x as num, name as text)
			var/player/p = mob
			chars_left += length(p.buttons[x]["text"])
			update_chars_left()
			p.buttons[x] = null
			p.buttons = shift_list(p.buttons)
			redraw_buttons()
			winset(src, "spell_create.[name]", "parent=null")

	proc
		set_up_spell_window(tier)
			var/player/p = mob
			var/book/b = p.BOOK

			mob.can_move = 0
			p.set_up_spell_grids()

			chars_left = b.length

			spell_error("")
			update_chars_left()
			clear_spell_buttons()
			update_spell_icon()

			winset(src, "spell_create.try","command=\"try [tier]\"")

			winshow(src, "spell_create")

		clear_spell_buttons()
			var/player/p = mob
			for(var/list/l in p.buttons)
				winset(src, "spell_create.[l["id"]]", "parent=null")
			p.buttons = new/list()

		update_spell_icon()

			var/string
			var/player/p = mob
			for(var/list/l in p.buttons)
				string += l["text"]

			var/list/l = generate_spell(lowertext(string), 3, mob)

			if(l == null || l.len == 0)
				winset(src, "spell_create.image","image=''")
				return

			var/spell/s = new/spell()
			s.initialize(l, mob)

			if(!s)
				winset(src, "spell_create.image","image=''")
				return

			var/f = fcopy_rsc(icon(s.icon, s.icon_state))
			winset(src, "spell_create.image","image=\"\ref[f]\"")
			del s

		redraw_buttons()
			var/player/p = mob
			for(var/x=1 to p.buttons.len)
				var/list/l = p.buttons[x]
				var/list/previousbutton = x>1 ? p.buttons[x-1] : \
					params2list(winget(src, "spell_create.button","pos;width;height;parent;size"))
				var/list/ppos= dd_text2list(previousbutton["pos"],",")
				var/list/size= dd_text2list(previousbutton["size"], "x")
				l["pos"] = "[text2num(ppos[1]) + text2num(size[1])]"
				winset(src, "spell_create.[l["id"]]", "pos=[l["pos"]],206")
				winset(src, "spell_create.[l["id"]]", "command=\"remove-spell-button [x] [l["id"]]\"")

		spell_error(error)
			winset(src, "spell_create.error","text=\"[error]\"")

		update_chars_left()
			winset(src, "spell_create.characters","text=\"Characters left: [chars_left]\"")
			update_spell_icon()

		add_component_button(new_word)

			if(length(new_word) > chars_left)
				spell_error("The component will not fit in the spell.")
				return

			var/player/p = mob
			if(!p.buttons) p.buttons = new /list()
			var/list/newbutton = new /list()

			var/list/previousbutton = p.buttons.len ? (length(dd_text2list(p.buttons[p.buttons.len]["pos"], ","))>1 ? p.buttons[p.buttons.len] : p.buttons[p.buttons.len-1]): \
				params2list(winget(src, "spell_create.button","pos;width;height;parent;size"))

			world << previousbutton["pos"]

			newbutton["type"] = "button"

			var/list/pos = dd_text2list(previousbutton["pos"], ",")
			var/list/size= dd_text2list(previousbutton["size"], "x")

			if(!(pos.len > 1))
				pos[1] = "[text2num(pos[1])+text2num(size[1])]"
				pos += "206"
			if(!(size.len > 1))
				world << "SIZE FAILED PLEASE DEBUG ME"
				return

			newbutton["pos"] = "[text2num(pos[1]) + text2num(size[1])],[pos[2]]"

			newbutton["is-visible"] = "true"

			newbutton["parent"] = "spell_create"

			newbutton["text"] = new_word

			newbutton["width"] = (length(new_word)*8)+5

			newbutton["size"] = "[newbutton["width"]]x[size[2]]"

			//god, making these things unique kinda pissed me off.
			newbutton["id"] = "[md5("spell[new_word][p.buttons.len+1][newbutton["width"]][rand(-1000001,1000000)]")]"

			newbutton["command"] = "remove-spell-button [p.buttons.len+1] [newbutton["id"]]"

			p.buttons += "temp"
			p.buttons[p.buttons.len]=newbutton

			chars_left -= length(new_word)
			update_chars_left()

			winset(src, newbutton["id"] ,list2params(newbutton))